home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / fish / 676-700 / 696 / clitools / touch / touch.c < prev    next >
C/C++ Source or Header  |  1995-03-18  |  2KB  |  105 lines

  1. ;/* touch - compiles with SAS/C 5.10a by executing this file
  2. lc -cfis -v -b0 -O -j73 touch
  3. blink touch.o to touch
  4. quit ;*/
  5. /*
  6. *    touch.c - touches files (makes their date NOW) - v1.0
  7. *
  8. *    Usage:    touch <file-list>
  9. *
  10. *    Handles standard AmigaDOS wildcards. If a given file doesn't exist (and
  11. *    it's not a pattern) the file is created with size zero.
  12. *
  13. *    Martin W. Scott, 4/92.
  14. *
  15. *    v1.01 - added notification of file creation (12/5/92).
  16. */
  17. #include <exec/types.h>
  18. #include <libraries/dos.h>
  19. #include <dos/dosasl.h>
  20. #include <dos/rdargs.h>
  21.  
  22. #include <proto/exec.h>        /* use PRAGMAS */
  23. #include <proto/dos.h>
  24.  
  25. void    createfile(char *), touch(char *),
  26.     touchall(char *), main(void);
  27.  
  28. char version_str[] = "$VER: touch v1.01";
  29.  
  30. struct DosLibrary *DOSBase;
  31. struct DateStamp now;            /* indeed */
  32. struct AnchorPath __aligned ap;        /* for Match(First|Next|End) */
  33.  
  34.  
  35. void main(void)        /* entry: touch files (set their date to NOW) */
  36. {
  37.     struct RDArgs *readargs;
  38.     LONG rargs[1];
  39.     char **strings;
  40.  
  41.     if (DOSBase = (struct DosLibrary *)OpenLibrary("dos.library", 37L))
  42.     {
  43.         if (readargs = ReadArgs("FILES/A/M", rargs, NULL))
  44.         {
  45.             strings = (char **)(rargs[0]);
  46.  
  47.             DateStamp(&now);
  48.             while (*strings)
  49.             {
  50.                 touchall(*strings);
  51.                 strings++;
  52.             }
  53.         }
  54.         else PrintFault(IoErr(), "touch");
  55.  
  56.         CloseLibrary(DOSBase);
  57.     }
  58.  
  59. } /* main */
  60.  
  61.  
  62. void createfile(char *file)        /* touch named file in current directory */
  63. {
  64.     BPTR fh;
  65.  
  66.     if (fh = Open(file, MODE_NEWFILE))
  67.     {
  68.         Close(fh);
  69.         PutStr("Creating file ");
  70.         PutStr(file);
  71.         PutStr("\n");
  72.     }
  73.     else PrintFault(IoErr(), "touch");
  74. }
  75.  
  76. void touch(char *file)        /* touch named file in current directory */
  77. {
  78.     if (!SetFileDate(file, &now))
  79.         PrintFault(IoErr(), "touch");
  80. }
  81.  
  82. void touchall(char *pat)    /* touch all files matching pattern */
  83. {
  84.     BPTR oldcd;
  85.     LONG err;
  86.  
  87.     if ((err = MatchFirst(pat, &ap)) == 0)    /* success */
  88.     {
  89.         do {
  90.             oldcd = CurrentDir(ap.ap_Current->an_Lock);
  91.             touch(ap.ap_Info.fib_FileName);
  92.             CurrentDir(oldcd);
  93.             err = MatchNext(&ap);
  94.         } while (!err);
  95.     }
  96.  
  97.     if (!(ap.ap_Flags & APF_ITSWILD) && (err == ERROR_OBJECT_NOT_FOUND))
  98.         createfile(pat);
  99.     else if (err != ERROR_NO_MORE_ENTRIES)    /* abnormal error */
  100.         PrintFault(err, "touch");
  101.  
  102.     MatchEnd(&ap);
  103. }
  104.  
  105.